package io.gitee.kotle.player.ui.view

import android.view.GestureDetector
import android.view.MotionEvent
import android.view.View
import androidx.core.view.GestureDetectorCompat
import kotlin.math.abs

open class PlayerGestureDetectorHelper(private val view: View) :
    GestureDetector.SimpleOnGestureListener() {
    /**
     * 当前的托管对象
     */
    private val gesture by lazy { GestureDetectorCompat(view.context, this) }

    /**
     * 是否支持手势调节功能
     */
    var enableGesture: Boolean = true

    /**
     * 不允许触摸区域的比例
     */
    var notAllowedTouchWidth = 0.1f

    /**
     * 不允许触摸区域的比例
     */
    var notAllowedTouchHeight = 0.1f

    /**
     * 当时是否是水平滑动
     * 水平滑动调节进度
     * 竖直滑动调节亮度或者音量
     */
    private var currentSwipeHorizontal: Boolean? = null

    /**
     * 是否在左边滑动
     */
    private var currentSwipeLeft: Boolean? = null

    /**
     * 记录滑动值
     */
    private var totalDistance = 0f


    /**
     * 需要给外部调用
     */
    open fun onTouchEvent(event: MotionEvent?): Boolean {
        event ?: return false
        if (!enableGesture) {
            return false
        }
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                if (isNotAllowedTouch(event)) {
                    //不在触摸区域，不响应任何事件，防止误触
                    return false
                }
            }

            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                //回调是否在左侧或者右侧滑动
                currentSwipeHorizontal?.let {
                    if (it) {
                        swipeHorizontal(totalDistance, 0f, MotionEvent.ACTION_UP)
                    } else {
                        currentSwipeLeft?.let { swipeLeft ->
                            if (swipeLeft) {
                                swipeLeft(totalDistance, 0f, MotionEvent.ACTION_UP)
                            } else {
                                swipeRight(totalDistance, 0f, MotionEvent.ACTION_UP)
                            }
                        }
                    }
                }
            }
        }
        return gesture.onTouchEvent(event)
    }

    private fun isNotAllowedTouch(event: MotionEvent?): Boolean {
        event ?: return true
        return event.x < notAllowedTouchWidth * view.width
                || event.x > view.width * (1 - notAllowedTouchWidth)
                || event.y < notAllowedTouchHeight * view.height
                || event.y > view.height * (1 - notAllowedTouchHeight)
    }

    override fun onDown(e: MotionEvent): Boolean {
        currentSwipeHorizontal = null
        currentSwipeLeft = null
        totalDistance = 0f
        return enableGesture
    }


    override fun onScroll(
        downEvent: MotionEvent?,
        moveEvent: MotionEvent,
        distanceX: Float,
        distanceY: Float
    ): Boolean {
        downEvent ?: return false
        //判断当前滑动方向
        val action =
            if (currentSwipeHorizontal == null) MotionEvent.ACTION_DOWN else MotionEvent.ACTION_MOVE
        val swipeHorizontal = currentSwipeHorizontal
            ?: (abs(distanceX) > abs(distanceY)).also {
                currentSwipeHorizontal = it
            }
        val swipeLeft = currentSwipeLeft
            ?: (downEvent.x < view.width / 2f).also {
                currentSwipeLeft = it
            }
        if (swipeHorizontal) {
            //水平滑动
            totalDistance += distanceX
            swipeHorizontal(totalDistance, distanceX, action)
        } else {
            //竖直滑动
            totalDistance += distanceY
            if (swipeLeft) {
                //在左区域竖直滑动
                swipeLeft(totalDistance, distanceY, action)
            } else {
                //在右区域竖直滑动
                swipeRight(totalDistance, distanceY, action)
            }
        }
        return true
    }


    /**
     * 水平滑动
     */
    open fun swipeHorizontal(totalDistance: Float, currentOffset: Float, action: Int) {

    }

    /**
     * 在右边滑动
     */
    open fun swipeLeft(totalDistance: Float, currentOffset: Float, action: Int) {

    }

    /**
     * 在左边滑动
     */
    open fun swipeRight(totalDistance: Float, currentOffset: Float, action: Int) {

    }


}